import LGXCOM from '../common/common.js'
import LXCoord from '../common/struct/coord.js'

import {LGXEnums}  from '../common/sysenum.js'


import globalConst from '../common/globalConst.js'
var global = globalConst.GlobalConst;

import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

class LayoutGraphSvc {
    constructor(datamanger) {
        this.dataManager = datamanger;
    }

    layout(kind) {
        console.log("layout graphs *******************************");
        var pCurentGraph = this.dataManager.getCurentGraph();
        if (!pCurentGraph) {
            return -1;
        }
        var graphList = [];
        graphList.push(pCurentGraph);
        this.dataManager.m_pOperStepService.record(1,graphList);
        var ret = this.layoutGraph(pCurentGraph, kind);
        this.dataManager.m_pOperStepService.record(0,graphList);
        return ret;
    }

    getOverlapGraphs(pCurentGraph){
        let overlapGraphLst = [];
        let tAllGraphLst = this.dataManager.getAllGraphs();
        for (let i = 0;i < tAllGraphLst.length;i++){
            let p = tAllGraphLst[i];
            if (funcs.chkOverlap(pCurentGraph.getZone(),p.getZone())){
                overlapGraphLst.push(p);
            }
        }
        return overlapGraphLst;
    }
    layoutGraph(dstGraph, kind) {
        let graphList = this.getOverlapGraphs(dstGraph);
        if (graphList < 2){
            return -1;
        }
        let sortGraphList = graphList.sort(function (p1, p2) {
            let inlayIdx1 = p1.getInlayerIdx();
            let inlayIdx2 = p2.getInlayerIdx();
            if ((inlayIdx1 - inlayIdx2) > 0) {
                return 1;
            }
            else {
                return -1;
            }
        }
        );

        switch (kind) {
            case "up":
                this.lay_upward(dstGraph,sortGraphList);
                break;

            case "down":
                this.lay_downward(dstGraph,sortGraphList);
                break;

            case "to_bottom":
                this.lay_toBottom(dstGraph,sortGraphList);
                break;

            case "to_top":
                this.algin_toTop(dstGraph,sortGraphList);
                break;
        }
    }

    lay_upward(dstGraph,graphList) {
        let idx1 = graphList.indexOf(dstGraph);
        if (idx1 == graphList.length - 1){
            return 0;
        }
        let layer = dstGraph.m_layer;
        let close2DstIdx = idx1 + 1;
        let close2DstGraph = graphList[close2DstIdx];
        let nxtIdx = close2DstGraph.getInlayerIdx();
        layer.moveGraphUpward2DstIdx(dstGraph,nxtIdx);
    }

    lay_downward(dstGraph,graphList) {
        let idx1 = graphList.indexOf(dstGraph);
        if (idx1 == 0){
            return 0;
        }
        let layer = dstGraph.m_layer;
        let close2DstIdx = idx1 - 1;
        let close2DstGraph = graphList[close2DstIdx];
        let nxtIdx = close2DstGraph.getInlayerIdx();
        layer.moveGraphUpward2DstIdx(dstGraph,nxtIdx);
    }

    lay_toBottom(dstGraph,graphList) {
        let idx1 = graphList.indexOf(dstGraph);
        if (idx1 == 0){
            return 0;
        }
        let layer = dstGraph.m_layer;
        layer.moveGraphToBottom(dstGraph);
    }

    algin_toTop(dstGraph,graphList) {
        let idx1 = graphList.indexOf(dstGraph);
        if (idx1 == length - 1){
            return 0;
        }
        let layer = dstGraph.m_layer;
        layer.moveGraphToTop(dstGraph);
    }
}

export default { LayoutGraphSvc }